/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.platform.wizards;

import com.patientis.framework.scripting.ServiceUtility;

import com.patientis.model.common.DateTimeModel;
import com.patientis.model.common.IBaseModel;
import com.patientis.model.patient.PatientModel;
import com.patientis.model.reference.GenderReference;
import com.patientis.model.reference.PhoneTypeReference;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;

import org.reliablesource.symbiosys.platform.viewers.SymbiosysControlFactory;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 
 * RegisterPatientWizard.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class RegisterPatientWizard implements INewWizard {
    IStructuredSelection selection;
    IWorkbenchPart currentPart;
    IWorkbenchWindow window;
    IWorkbench workbench;
    ArrayList<IWizardPage> wizardPages;
    Composite pageContainer;
    IWizardPage currentPage;
    IWizardContainer wizardContainer;
    IWizardPage startPage;
    IDialogSettings dialogSettings;
    PatientModel patientModel;

    public RegisterPatientWizard() {
	wizardPages = new ArrayList<IWizardPage>();
	dialogSettings = new RegisterNewPatientDialogSettings();
    }

    public void init(IWorkbench _workbench, IStructuredSelection _selection) {
	workbench = _workbench;
	selection = _selection;
    }

    public void addPages() {
	WizardPage _page = new RegisterNewPatientWizardPage();
	wizardPages.add(_page);
	currentPage = _page;
	startPage = _page;
    }

    public boolean canFinish() {
	//        if(patientModel != null){
	//            
	//            try{
	//                patientModel.validateDataModel();
	//            }catch(final ISValidateControlException _isvce){
	//                System.err.println(_isvce.getLocalizedMessage() + " \n\nCause:  " + _isvce.getCause().getLocalizedMessage());
	//                wizardContainer.updateMessage();
	//            }
	//            
	//        }

	//        boolean first = ((Text)SymbiosysControlFactory.getInstance().patientControls.get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.FIRST])).getText().length() > 0;
	//        boolean last = ((Text)SymbiosysControlFactory.getInstance().patientControls.get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.LAST])).getText().length() > 0;
	//        boolean phone = ((Text)SymbiosysControlFactory.getInstance().patientControls.get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.LAST])).getText().length() > 0;
	//        boolean address = ((Text)SymbiosysControlFactory.getInstance().patientControls.get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.LAST])).getText().length() > 0;

	//        return first & last & phone & address;
	return true;
    }

    public void createPageControls(Composite _pageContainer) {
	pageContainer = _pageContainer;

	Iterator<IWizardPage> _it = wizardPages.iterator();

	while (_it.hasNext()) {
	    WizardPage _pg = (WizardPage) ((IWizardPage) _it.next());
	    _pg.createControl(_pageContainer);
	}
    }

    public void dispose() {
	Iterator<IWizardPage> _it = wizardPages.iterator();

	while (_it.hasNext()) {
	    WizardPage _pg = (WizardPage)  _it.next();
	    _pg.dispose();
	}

	wizardPages.clear();
    }

    public IWizardContainer getContainer() {
	return wizardContainer;
    }

    public Image getDefaultPageImage() {
	return null;
    }

    public IDialogSettings getDialogSettings() {
	return dialogSettings;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
     */
    public IWizardPage getNextPage(IWizardPage page) {
	IWizardPage[] _pgs = getPages();

	for (int i = 0; i < _pgs.length; i++) {
	    if (page.equals(_pgs[i]))
		return _pgs[i].getNextPage();
	}

	return null;
    }

    public IWizardPage getPage(String _pageName) {
	IWizardPage[] _pgs = getPages();

	for (int i = 0; i < _pgs.length; i++) {
	    if (_pageName.equals(_pgs[i].getName()))
		return _pgs[i];
	}

	return null;
    }

    public int getPageCount() {
	return wizardPages.size();
    }

    public IWizardPage[] getPages() {
	return wizardPages.toArray(new IWizardPage[wizardPages.size()]);
    }

    public IWizardPage getPreviousPage(IWizardPage page) {
	return page.getPreviousPage();
    }

    public IWizardPage getStartingPage() {
	return startPage;
    }

    public RGB getTitleBarColor() {
	return null;
    }

    public String getWindowTitle() {
	return "Register a New Patient";
    }

    public boolean isHelpAvailable() {
	return false;
    }

    public boolean needsPreviousAndNextButtons() {
	return false;
    }

    public boolean needsProgressMonitor() {
	return false;
    }

    public boolean performCancel() {
	return true;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.wizard.IWizard#performFinish()
     */
    public boolean performFinish() {
	if (patientModel == null)
	    patientModel = new PatientModel();

	patientModel
		.setFirstName(((Text) SymbiosysControlFactory.getInstance().patientControls
			.get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.FIRST]))
			.getText());
	patientModel
		.setLastName(((Text) SymbiosysControlFactory.getInstance().patientControls
			.get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.LAST]))
			.getText());

	patientModel.setBirthDt(DateTimeModel.getNow().getAddDays(900));

	try {
	    patientModel.setGenderRef(ServiceUtility.getRef(GenderReference
		    .groupName(), "MALE"));
	    patientModel
		    .givePhone(
			    ServiceUtility.getRef(
				    PhoneTypeReference.groupName(), "HOME")
				    .getId())
		    .setPhoneNumber(
			    ((Text) SymbiosysControlFactory.getInstance().patientControls
				    .get(SymbiosysControlFactory.PATIENT[SymbiosysControlFactory.HOME_PHONE]))
				    .getText().trim());
	} catch (Exception _e) {
	}

	return true;
    }

    public void setContainer(final IWizardContainer _wizardContainer) {
	wizardContainer = _wizardContainer;
    }

    public IBaseModel getUserInputModel() {
	return patientModel;
    }
}
